<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Prettus\Repository\Contracts\Transformable;
use Prettus\Repository\Traits\TransformableTrait;

/**
 * Class DormMeterAdjust.
 *
 * @package namespace App\Models;
 */
class DormMeterAdjust extends BaseModel implements Transformable
{
    use TransformableTrait;

    protected $appends = ['adjust_type'];

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['dorm_id', 'meter_type_id', 'dorm_meter_id_old', 'dorm_meter_id_new', 'adjust_date', 'remark'];

    public function dorm()
    {
        return $this->belongsTo(Dorm::class);
    }
    public function meterType()
    {
        return $this->belongsTo(MeterType::class);
    }

    public function dormMeterOld()
    {
        return $this->belongsTo(DormMeter::class, 'dorm_meter_id_old');
    }

    public function dormMeterNew()
    {
        return $this->belongsTo(DormMeter::class, 'dorm_meter_id_new');
    }

    public function getAdjustTypeAttribute()
    {
        if (array_has($this->attributes, 'dorm_meter_id_old') && $this->attributes['dorm_meter_id_old']) {
            return '更换';
        } else {
            return '安装';
        }
    }
}
